/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controller;

import Utility.Utility;
import beans.ErrorBean;
import beans.LoginForm;
import beans.UserType;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import xmlMapping.Ban;
import xmlMapping.Forum;
import xmlMapping.Member;
import xmlMapping.Moderatorpermessions;
import xmlMapping.Suspend;
import net.tanesha.recaptcha.ReCaptchaImpl;
import net.tanesha.recaptcha.ReCaptchaResponse;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMessage;
/**
 *
 * @author sh
 */
public class LoginAction extends org.apache.struts.action.Action {
    
    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    
    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        Moderatorpermessions modpermessions=null;
         ErrorBean errorBean=new ErrorBean();
        Utility utility=new Utility();
        Member mem=new Member();
        mem.setUserName(((LoginForm)form).getUsername());
        Utility ut=new Utility();
       String encrypted= ut.encrypt(((LoginForm)form).getPass());
      // mem.setPassword(((LoginForm)form).getPass());
       mem.setPassword(encrypted);
            String remoteAddr = request.getRemoteAddr();
        ReCaptchaImpl reCaptcha = new ReCaptchaImpl();
        reCaptcha.setPrivateKey("6LdNesQSAAAAAEXUT7A-rvpKCtdLTqZVEoSuDx8d");
         ActionErrors errors = new ActionErrors();
        String challenge = request.getParameter("recaptcha_challenge_field");
        String uresponse = request.getParameter("recaptcha_response_field");
        ReCaptchaResponse reCaptchaResponse = reCaptcha.checkAnswer(remoteAddr, challenge, uresponse);

        if (!reCaptchaResponse.isValid()) {
         
            System.out.println(" invalid caption ");
         errors.add("invalidCap", new ActionMessage("InvalidCaption"));


                saveErrors(request, errors);

               return mapping.findForward("failure");
        }
        Member member=utility.login(mem);
        if(member != null){
            Ban ban=utility.checkBan(member.getMemberId());
            System.out.println("ban//////////"+ban);
            if(ban!=null){
                errorBean.setErrorMsg(ban.getReason());
                errorBean.setDate(ban.getDatebanned().toString());
                errorBean.setType("Ban");
                request.setAttribute("error", errorBean);
                return mapping.findForward("error");
            }else{
                Suspend sus=utility.checkSuspend(member.getMemberId());
                System.out.println("sus//////////"+sus);
                if(sus!=null){
                    errorBean.setErrorMsg(sus.getReason());
                    errorBean.setDate(sus.getExpiredate().toString());
                    errorBean.setType("Suspend");
                    request.setAttribute("error", errorBean);
                    return mapping.findForward("error");
                }else{
                    UserType type=null;
                    if(member.getType().equals("adm")){
                        type=UserType.adm;
                    }else{
                        if(member.getType().equals("supermod")){
                            type=UserType.supermod;
                            modpermessions=member.getModeratorpermessions();
                        }else{
                            if(member.getType().equals("mod")){
                                type=UserType.mod;
                                modpermessions=member.getModeratorpermessions();
                                Set forums=member.getForums();
                                request.getSession(true).setAttribute("forums",forums);
                               // System.out.println("XXXXXXXXXXXXXXXffff"+((Forum)forums.toArray()[0]).getForumId());
                            }else{
                                if(member.getType().equals("mem")){
                                    type=UserType.mem;
                                }
                            }
                        }
                    }
                    request.getSession(true).setAttribute("type", type);
                    request.getSession(true).setAttribute("username", member.getUserName());
                    request.getSession(true).setAttribute("id", member.getMemberId());
                    request.getSession(true).setAttribute("permessions",modpermessions);

                    return mapping.findForward("success");
                }
            }
            //request.getSession(true).setAttribute("user", member);
        }

        errorBean.setErrorMsg("Error in user name or password");
        request.setAttribute("error", errorBean);
        return mapping.findForward("failure");
    }
}
